﻿Public Class RECEIPTTYPE
    Dim connect As CONDB = CONDB.NewConnection
    Dim s As FILTERCLASS
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub TMPIDTxt2_Click(sender As Object, e As EventArgs) Handles TMPIDTxt2.Click
        TMPIDTxt2.Text = ""
        TMPIDTxt2.Tag = ""
    End Sub

    Private Sub BILLIDTxt2_Click(sender As Object, e As EventArgs) Handles BILLIDTxt2.Click
        BILLIDTxt2.Text = ""
        BILLIDTxt2.Tag = ""
    End Sub

    Private Sub CCODETxt2_Click(sender As Object, e As EventArgs) Handles CCODETxt2.Click
        CCODETxt2.Text = ""
        CCODETxt2.Tag = ""
    End Sub


    Private Sub RECEIPTTYPE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridclassreceipt()
        SearchTMPIDTxt2()
        SearchBILLIDTxt2()
        SearchCCODETxt2()
        SearchRCODETxt2()
        RCODETxt2.Enabled = False
        AddHandler CLASSRECEIPTGrid.GetCellStyle, AddressOf Me.SuperGridControl1GetCellStyle
        Me.Refresh()

        DesignForm(Me)
        DesignGrid(CLASSRECEIPTGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SearchTMPIDTxt2()
        Dim sql2 As String
        sql2 = "SELECT cast(`tmpid` as nchar(3)) as tmpid,`tempname`FROM tempbilling WHERE `STATUS` = 1 "
        s = New FILTERCLASS(TMPIDTxt2, sql2, "รหัส,แบบหมวดลำดับใบเสร็จ", "70,143", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub
    Private Sub SearchBILLIDTxt2()
        Dim sql2 As String
        sql2 = "SELECT cast(`billid` as nchar(3)) as billid2,`bname_th`FROM masbilling WHERE `STATUS` = 1 "
        s = New FILTERCLASS(BILLIDTxt2, sql2, "รหัส,ลำดับใบเสร็จ", "50,163", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub
    Private Sub SearchCCODETxt2()
        Dim sql2 As String
        sql2 = "SELECT cast(`ccode` as nchar(3)) as ccode,`name`FROM finchcode WHERE `STATUS` = 1 "
        s = New FILTERCLASS(CCODETxt2, sql2, "รหัส,รหัสรายได้", "50,163", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub
    Private Sub SearchRCODETxt2()
        Dim sql2 As String
        sql2 = "SELECT cast(`rcode` as nchar(3)) as rcode,`name`FROM finrecode WHERE `STATUS` = 1 "
        s = New FILTERCLASS(RCODETxt2, sql2, "รหัส,รหัสรายรับทางการเงิน", "50,163", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub

    Private Sub SuperGridControl1GetCellStyle(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridGetCellStyleEventArgs)
        If e.GridCell.GridColumn.Name.Equals("ลำดับการแสดง") = True Then
            ''If CStr(e.GridCell.Value).Equals("True") = True Then
            'e.GridCell.GridRow.CellStyles.Default.Background.Color1 = Color.Orange
            'e.GridCell.GridRow.Item(3).CellStyles.Default.Background.Color1 = Color.Orange
            ''End If
        End If
    End Sub

    Private Sub SelectDatagridclassreceipt()
        Dim objSelectDataCLASSRECEIPT As New CLASSRECEIPT
        Dim CLASSRECEIPT As DataTable
        CLASSRECEIPT = connect.GetTable(objSelectDataCLASSRECEIPT.SelectClassreceipt_)
        CLASSRECEIPTGrid.PrimaryGrid.DataSource = CLASSRECEIPT
    End Sub

    Private Sub cleardata()
        TMPIDTxt2.Text = ""
        CLSIDTxt.Text = ""
        BILLIDTxt2.Text = ""
        CCODETxt2.Text = ""
        RCODETxt2.Text = ""
        classreceiptstatuscheck.Checked = False
    End Sub

    Private Sub CCODETxt2_TextChanged(sender As Object, e As EventArgs) Handles CCODETxt2.TextChanged
        If Convert.ToString(CCODETxt2.Tag).Trim = "" Then
            RCODETxt2.Enabled = False
            Return
        End If
        RCODETxt2.Enabled = True
        Dim sql2 As String
        sql2 = "SELECT cast(`rcode` as nchar(3)) as rcode,`name`FROM finrecode WHERE `ccode` = '" & Convert.ToString(CCODETxt2.Tag).Trim & "'  and  `STATUS` = 1 "
        s = New FILTERCLASS(RCODETxt2, sql2, "รหัส,รหัสรายรับทางการเงิน", "50,163", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub

    Private Sub TMPIDTxt2_TextChanged(sender As Object, e As EventArgs) Handles TMPIDTxt2.TextChanged
        Dim sql As String
        Dim dt As New DataTable
        If TMPIDTxt2.Text.Length > 0 Then
            sql = "SELECT `clsid` as 'รหัส' ,tempbilling.`tmpid`,tempbilling.`tempname` as 'แบบลำดับใบเสร็จ' ,`seq_disp` as 'ลำดับการแสดง',masbilling.`billid` as 'billid2',masbilling.`bname_th` as 'ลำดับใบเสร็จ',finchcode.`ccode`,finchcode.`name` as 'รหัสรายได้' ,finrecode.`rcode`,finrecode.`name` as 'รหัสรายรับทางการเงิน',`status` as 'สถานะการเปิดใช้งาน' FROM classreceipt LEFT JOIN (SELECT `tmpid`,`tempname` FROM tempbilling WHERE `status` = 1) as tempbilling ON classreceipt.`tmpid` = tempbilling.`tmpid` LEFT JOIN (SELECT `billid`,`bname_th` FROM masbilling WHERE `status` = 1) as masbilling ON classreceipt.`billid` = masbilling.`billid`LEFT JOIN (SELECT `ccode`,`name` FROM finchcode WHERE `status` = 1) as finchcode ON classreceipt.`ccode` = finchcode.`ccode`LEFT JOIN (SELECT `rcode`,`name` FROM finrecode WHERE `status` = 1) as finrecode ON classreceipt.`rcode` = finrecode.`rcode` where classreceipt.`tmpid` = '" & Convert.ToString(TMPIDTxt2.Tag).Trim & "'"
            dt = connect.GetTable(sql)
            CLASSRECEIPTGrid.PrimaryGrid.DataSource = connect.GetTable(sql)
        Else
            sql = "SELECT `clsid` as 'รหัส' ,tempbilling.`tmpid`,tempbilling.`tempname` as 'แบบลำดับใบเสร็จ' ,`seq_disp` as 'ลำดับการแสดง',masbilling.`billid` as 'billid2',masbilling.`bname_th` as 'ลำดับใบเสร็จ',finchcode.`ccode`,finchcode.`name` as 'รหัสรายได้' ,finrecode.`rcode`,finrecode.`name` as 'รหัสรายรับทางการเงิน',`status` as 'สถานะการเปิดใช้งาน' FROM classreceipt LEFT JOIN (SELECT `tmpid`,`tempname` FROM tempbilling WHERE `status` = 1) as tempbilling ON classreceipt.`tmpid` = tempbilling.`tmpid` LEFT JOIN (SELECT `billid`,`bname_th` FROM masbilling WHERE `status` = 1) as masbilling ON classreceipt.`billid` = masbilling.`billid`LEFT JOIN (SELECT `ccode`,`name` FROM finchcode WHERE `status` = 1) as finchcode ON classreceipt.`ccode` = finchcode.`ccode`LEFT JOIN (SELECT `rcode`,`name` FROM finrecode WHERE `status` = 1) as finrecode ON classreceipt.`rcode` = finrecode.`rcode`"
            dt = connect.GetTable(sql)
            CLASSRECEIPTGrid.PrimaryGrid.DataSource = connect.GetTable(sql)
        End If
    End Sub

    Private Sub CLASSRECEIPTGrid_EndEdit(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridEditEventArgs) Handles CLASSRECEIPTGrid.EndEdit
        'MsgBox(e.GridCell.RowIndex)
        Dim rowIndex As Integer = -1
        Dim cellVal As Integer = -1
        Dim flag As Boolean = False
        cellVal = e.GridCell.Value
        rowIndex = e.GridCell.RowIndex
        Dim sql As String
        sql = "UPDATE classreceipt SET `seq_disp` = '" & cellVal & "' WHERE `clsid` = '" & CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(0).Value & "' "
        connect.ExecuteNonQuery(sql)
        'CLASSRECEIPTGrid.PrimaryGrid.de
        For i As Integer = 0 To CLASSRECEIPTGrid.PrimaryGrid.Rows.Count - 1

            If CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(7).Value = CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(7).Value And i <> rowIndex And CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(2).Value = cellVal Then
                CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.Red
                CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.Red
                flag = True
            Else

                If flag = False Then
                    CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Nothing

                    CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Nothing
                End If
            End If
            'Else
            'CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Nothing

            'If CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(7).Value <> CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(7).Value And i = rowIndex And CType(CLASSRECEIPTGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells(2).Value <> cellVal Then
            '    CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Nothing
            'Else
            '    CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(rowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.Red
            '    CType(CLASSRECEIPTGrid.PrimaryGrid.GridPanel.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.Red

            'End If
        Next
        CLASSRECEIPTGrid.PrimaryGrid.SetSort({CLASSRECEIPTGrid.PrimaryGrid.Columns(2), CLASSRECEIPTGrid.PrimaryGrid.Columns(7)}, DevComponents.DotNetBar.SuperGrid.SortDirection.Ascending)
    End Sub

    Private Sub CLASSRECEIPTGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles CLASSRECEIPTGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("tmpid").Value() IsNot DBNull.Value Then
            TMPIDTxt2.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("tmpid").Value()
        Else
            TMPIDTxt2.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แบบลำดับใบเสร็จ").Value() IsNot DBNull.Value Then
            TMPIDTxt2.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แบบลำดับใบเสร็จ").Value()
        Else
            TMPIDTxt2.Text = ""
        End If
        'MsgBox(CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("billid").Value())
        If grid.Cells("billid2").Value() IsNot DBNull.Value Then
            BILLIDTxt2.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("billid2").Value()
        Else
            BILLIDTxt2.Tag = ""
        End If
        If grid.Cells("ลำดับใบเสร็จ").Value() IsNot DBNull.Value Then
            BILLIDTxt2.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ลำดับใบเสร็จ").Value()
        Else
            BILLIDTxt2.Text = ""
        End If
        If grid.Cells("ccode").Value() IsNot DBNull.Value Then
            CCODETxt2.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value()
        Else
            CCODETxt2.Tag = ""
        End If
        If grid.Cells("รหัสรายได้").Value() IsNot DBNull.Value Then
            CCODETxt2.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value()
        Else
            CCODETxt2.Text = ""
        End If
        If grid.Cells("rcode").Value() IsNot DBNull.Value Then
            RCODETxt2.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value()
        Else
            RCODETxt2.Tag = ""
        End If
        If grid.Cells("รหัสรายรับทางการเงิน").Value() IsNot DBNull.Value Then
            RCODETxt2.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Value()
        Else
            RCODETxt2.Text = ""
        End If
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            CLSIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            CLSIDTxt.Text = ""
        End If
        If CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            classreceiptstatuscheck.Checked = True
        Else
            classreceiptstatuscheck.Checked = False
        End If
        'connect = CONDB.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connect.BeginTrans()
        '        Sql = "UPDATE masgrpoper SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "`STATUS` = 1 "
        '        Else
        '            Sql += "`STATUS` = 0 "
        '        End If
        '        Sql += "WHERE `grpid` = '" & GRPIDTxt.Text & "' ;"
        '        connect.ExecuteNonQuery(Sql)
        '        connect.CommitTrans()
        '    Catch ex As Exception
        '        connect.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
    End Sub

    Private Sub addclassreceipt_Click(sender As Object, e As EventArgs) Handles addclassreceipt.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If TMPIDTxt2.Text.Length > 0 Then
                    Dim objInsertData As New CLASSRECEIPT
                    If Convert.ToString(BILLIDTxt2.Tag).Trim <> "" Then
                        objInsertData.BILLID2_ = Convert.ToString(BILLIDTxt2.Tag).Trim
                    End If
                    If Convert.ToString(CCODETxt2.Tag).Trim <> "" Then
                        objInsertData.CCODE2_ = Convert.ToString(CCODETxt2.Tag).Trim
                    End If
                    If Convert.ToString(RCODETxt2.Tag).Trim <> "" Then
                        objInsertData.RCODE2_ = Convert.ToString(RCODETxt2.Tag).Trim
                    End If
                    If Convert.ToString(TMPIDTxt2.Tag).Trim <> "" Then
                        objInsertData.TMPID2_ = Convert.ToString(TMPIDTxt2.Tag).Trim
                    End If
                    objInsertData.STATUS2_ = classreceiptstatuscheck.Checked
                    objInsertData.insertClassreceiptData()

                    SelectDatagridclassreceipt()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editclassreceipt_Click(sender As Object, e As EventArgs) Handles editclassreceipt.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If TMPIDTxt2.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objEditData As New CLASSRECEIPT
                        If Convert.ToString(TMPIDTxt2.Tag) <> "" Then
                            objEditData.TMPID2_ = Convert.ToString(TMPIDTxt2.Tag)
                        End If
                        If Convert.ToString(BILLIDTxt2.Tag) <> "" Then
                            objEditData.BILLID2_ = Convert.ToString(BILLIDTxt2.Tag)
                        End If
                        If Convert.ToString(CCODETxt2.Tag) <> "" Then
                            objEditData.CCODE2_ = Convert.ToString(CCODETxt2.Tag)
                        End If
                        If Convert.ToString(RCODETxt2.Tag) <> "" Then
                            objEditData.RCODE2_ = Convert.ToString(RCODETxt2.Tag)
                        End If
                        objEditData.CLSID_ = CLSIDTxt.Text
                        objEditData.STATUS2_ = classreceiptstatuscheck.Checked
                        objEditData.editClassreceiptdata()

                        SelectDatagridclassreceipt()

                        cleardata()
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                addclassreceipt.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("maslocationd")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("maslocationd")
        nextform.ShowDialog()
    End Sub
End Class